﻿using ConfExpert.Forms.Base;

using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraVerticalGrid.Rows;
using DevTeam;

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class AksesuarGirisPartileri : XForm
    {
        public AksesuarGirisPartileri()
        {
            InitializeComponent();
        }

        private void AksesuarGirisPartileri_Load(object sender, EventArgs e)
        {
            btnKaydet.Visibility = ytk(51) ? DevExpress.XtraBars.BarItemVisibility.Always : DevExpress.XtraBars.BarItemVisibility.Never;

            gvGirisPartisi.Init( "GetAllAksesuarGirisFiltered",
                intParse(GetVal("AksesuarGirenPartiId")),
                strParse(GetVal("IrsaliyeNo")),
                intParse(GetVal("KaynakId")),
                intParse(GetVal("CariId")),
                datetimeParse(GetVal("BaslangicTarihi")),
                datetimeParse(GetVal("BitisTarihi")),
                intParse(GetVal("Giren")),
                strParse(GetVal("Aciklama")),
                intParse(GetVal("AksesuarId")),
                intParse(GetVal("BrandId"))
                );
            gvGirisPartisi.AddDetailGrid(gvGirisPartisiLines, "AksesuarGirenPartiId");

            gvGirisPartisi.AddColumn("ToplamAdet", typeof(int), "SUM(Child({FK}).GelenAdet)");

            gvGirisPartisiLines.MakeColumnInvisible("AksesuarGirenPartiId");
            gvGirisPartisi.SetCaptions("AksesuarGirenPartiId:PartiNo", "IrsaliyeNumber:İrsaliye No", "UserId:Giren");
            gvGirisPartisi.YanYanaGetir("ToplamAdet:BelgeTipi", "GirisTipiId:IrsaliyeNumber");

            gvGirisPartisi.FormatAsNumber(0, "ToplamAdet");
            gvGirisPartisiLines.FormatAsNumber(0, "GelenAdet");

            gvGirisPartisi.MakeReadOnly("IrsaliyeNumber", "Aciklama", "BelgeNo");
            gvGirisPartisiLines.MakeReadOnly();

            if (ytk(51))
            {
                GridManager gm = GridManager.GetManager(gvGirisPartisi);
                gm.GridMenu.AddMenu("Girişi İptal Et", CancelStockIn);
            }

            splitContainerControl1.AutoSize();
            btnKaydet.ItemClick += btnKaydet_ItemClick;

            btnGetir.Click += btnGetir_Click;
            btnTemizle.Click += btnTemizle_Click;

            CreateVerticalGrid();


            gvGirisPartisi.AddSummary("ToplamAdet");
            gvGirisPartisiLines.AddSummary("GelenAdet");

            gvGirisPartisi.BestFitColumns();
            gvGirisPartisiLines.BestFitColumns();

        }

        void btnGetir_Click(object sender, EventArgs e)
        {
            if (ds.HasChanges())
                if (XtraMessageBox.Show("Değişiklikleri kaydetmek ister misiniz?", "Critical", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    btnKaydet.PerformClick();
            Rfrsh();
        }

        private void Rfrsh()
        {
            gvGirisPartisi.RefreshDataSource("GetAllAksesuarGirisFiltered",
                intParse(GetVal("AksesuarGirenPartiId")),
                strParse(GetVal("IrsaliyeNo")),
                intParse(GetVal("KaynakId")),
                intParse(GetVal("CariId")),
                datetimeParse(GetVal("BaslangicTarihi")),
                datetimeParse(GetVal("BitisTarihi")),
                intParse(GetVal("Giren")),
                strParse(GetVal("Aciklama")),
                intParse(GetVal("AksesuarId")),
                intParse(GetVal("BrandId"))
                );

            Notify("Done.");
        }

        private void CreateVerticalGrid()
        {
            vgc.Rows.AddRange(new BaseRow[] {
                AddItem("AksesuarGirenPartiId", typeof(int)),
                AddItem("IrsaliyeNo", typeof(string)),
                AddItem("KaynakId", typeof(int), RI(Rp.Kaynak)),
                AddItem("CariId", typeof(int), RI(Rp.Cari)),
                AddItem("BaslangicTarihi", typeof(DateTime), new RepositoryItemDateEdit()),
                AddItem("BitisTarihi", typeof(DateTime), new RepositoryItemDateEdit()),
                AddItem("Giren", typeof(int), RI(Rp.V_User)),
                AddItem("Aciklama", typeof(string), new RepositoryItemMemoEdit()),
                AddItem("AksesuarId", typeof(int), RI(Rp.V_Aksesuar)),
                AddItem("BrandId", typeof(int), RI(Rp.Brand))
            });

            vgc.Rows.GetRowByFieldName("Aciklama", true).Height = 60;

            vgc.OptionsBehavior.UseEnterAsTab = true;
            vgc.LayoutStyle = DevExpress.XtraVerticalGrid.LayoutViewStyle.SingleRecordView;
            vgc.CustomDrawRowHeaderCell += new DevExpress.XtraVerticalGrid.Events.CustomDrawRowHeaderCellEventHandler(vgc_CustomDrawRowHeaderCell);
            vgc.Appearance.RowHeaderPanel.BackColor = Color.LightBlue;

            vgc.SetCellValue(vgc.Rows["BaslangicTarihi"], 0, DateTime.Now.AddDays(-7));
            vgc.SetCellValue(vgc.Rows["BitisTarihi"], 0, DateTime.Now);

            vgc.FocusedRow = vgc.Rows["IrsaliyeNo"];
            vgc.BestFit();
        }

        public BaseRow AddItem(string name, Type t, params RepositoryItem[] ri)
        {
            return AddItem(vgc, name, t, ri);
        }

        object GetVal(string FieldName)
        {
            return vgc.GetCellValue(vgc.Rows[FieldName], vgc.FocusedRecord);
        }

        void btnTemizle_Click(object sender, EventArgs e)
        {
            foreach (BaseRow r in vgc.Rows)
                vgc.SetCellValue(r, 0, null);
        }
        
        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {            
            gvGirisPartisi.EndEditing();

            if (gvGirisPartisi.Dataset().HasChanges())
                gvGirisPartisi.Update("","AksesuarGirenParti");

            Rfrsh();
        }

        void CancelStockIn(object sender, EventArgs e)
        {
            int parti = gvGirisPartisi.GetFocusedRowCellValue("AksesuarGirenPartiId").ToInt();
            if (parti > 0)
            {
                bool IsDeleted = ExecuteScalar("DelAksesuarGirisPartisi", parti, WhoAmI).ToInt() == 1;
                if (IsDeleted)
                {
                    XtraMessageBox.Show(parti.ToString() + " nolu Giriş Partisi Silindi.");
                    Rfrsh();
                }
                else
                    ShowErrorMessage(parti.ToString() + " nolu giriş partisi SİLİNEMEDİ. Bunun sebebi girişi yapılmış olan aksesuarların çıkılmış olmasıdır.");
            }

        }

    }
}